﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace CommonLib.Log
{
    public class EfficientLogPool
    {
        private static EfficientLogPool instance;
        private static object _lockObj = new object();
        private static string _logPath = string.Empty;
        public static string LogPath
        {
            get { return _logPath; }
        }

        private List<EfficientLogger> _efficientLoggerList;
        public List<EfficientLogger> EfficientLoggerList
        {
            get { return _efficientLoggerList; }
        }

        private EfficientLogPool()
        {
            _efficientLoggerList = new List<EfficientLogger>();
            if (ConfigurationManager.AppSettings["CommonLog"] != null)
            {
                _logPath = ConfigurationManager.AppSettings["CommonLog"].ToString();
            }
        }

        public static EfficientLogPool GetInstance()
        {
            if (instance == null)
            {
                lock (_lockObj)
                {
                    if (instance == null)
                    {
                        instance = new EfficientLogPool();
                    }
                }
            }

            return instance;
        }

        public EfficientLogger GetEfficientLogger(int type, string toolsID)
        {
            return  _efficientLoggerList.FirstOrDefault(p => p.Type == type && p.DeviceName == toolsID);
        }

        public bool CreateEfficientLogger(string date, int type, string toolsID)
        {
            bool ret = false;

            try
            {
                EfficientLogger efficientLogger = GetEfficientLogger(type, toolsID);
                if (efficientLogger == null)
                {
                    string path = string.Format("{0}{1}\\{2}", _logPath, DateTime.Now.ToString("yyyyMMdd"), toolsID);
                    efficientLogger = new EfficientLogger(date, type, toolsID, path);
                    efficientLogger.InitLogFile();

                    AddEfficientLogger(efficientLogger);

                    Logger.getInstance().WriteToLog((int)LogType.I, "创建设备[{0}][{1}]文件流对象成功".FormatWith(toolsID, efficientLogger.Date), _logPath);
                }
            }
            catch (Exception ex)
            {
                Logger.getInstance().WriteToLog((int)LogType.E, "CreateEfficientLogger => " + ex.Message, _logPath);
            }

            return ret;
        }

        public bool DisposeEfficientLogger(int type, string toolsID)
        {
            bool ret = false;

            try
            {
                EfficientLogger efficientLogger = GetEfficientLogger(type, toolsID);
                if (efficientLogger != null)
                {
                    Logger.getInstance().WriteToLog((int)LogType.I, "释放设备[{0}][{1}]文件流对象成功".FormatWith(toolsID, efficientLogger.Date), _logPath);

                    RemoveEfficientLogger(efficientLogger);
                    efficientLogger.CloseLogFile();
                    efficientLogger = null;
                }
            }
            catch (Exception ex)
            {
                Logger.getInstance().WriteToLog((int)LogType.E, "DisposeEfficientLogger => " + ex.Message, _logPath);
            }

            return ret;
        }

        public void AddEfficientLogger(EfficientLogger efficientLogger)
        {
            if (!_efficientLoggerList.Contains(efficientLogger))
            {
                _efficientLoggerList.Add(efficientLogger);
            }
        }

        public void RemoveEfficientLogger(EfficientLogger efficientLogger)
        {
            if (_efficientLoggerList.Contains(efficientLogger))
            {
                _efficientLoggerList.Remove(efficientLogger);
            }
        }
    }
}
